﻿using MuseumApp315.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using MuseumApp315.Data;


namespace MuseumApp315.Adapters
{
    public class MuseumDataAdapter : IMuseumAdapter
    {
        public List<Museum> GetAll() { 
        //
            List<Museum> mList = new List<Museum>();

            using (ApplicationDbContext db = new ApplicationDbContext())
            {

                mList = db.Museums.Include(r => r.Reviews).ToList();

                var fResults = mList.Select(m => new
                {
                    MuseumId = m.MuseumId,
                    Name = m.Name,
                    City = m.City,
                    Description = m.Description,

                    //Tags = db.MuseumTags.Where(t => t.MuseumId == m.MuseumId).Select(t => new { Id = t.TagId, Name = t.Tag }).ToList(),

                    Reviews = m.Reviews.Select(r => new
                    {
                        ReviewId = r.ReviewId,
                        UserName = r.UserName
                    })
                });
                return (List<Museum>)fResults;

            }
        //
        }


        public IEnumerable<Museum> GetOne(int id)
        {
            List<Museum> mList = new List<Museum>();

            using (ApplicationDbContext db = new ApplicationDbContext())
            {

                mList = db.Museums.Where(m => m.MuseumId == id).Include(r => r.Reviews).ToList();

                var fResults = mList.Select(m => new
                {
                    MuseumId = m.MuseumId,
                    Name = m.Name,
                    City = m.City,
                    Description = m.Description,

                    Tags = db.MuseumTags.Where(t => t.MuseumId == id).Select(t => t.Tag).ToList(),

                    Reviews = m.Reviews.Select(r => new
                    {
                        ReviewId = r.ReviewId,
                        UserName = r.UserName,
                        Comment = r.Comment
                    })
                });
                return (IEnumerable < Museum >) fResults;

            }
        }




    }
}